package com.itheima;

import com.itheima.Utils.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet("/addTimeServlet")
public class addTimeServelt extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        Cookie cookie = CookieUtils.findCookieByName(request, "LastTime");
        if (cookie==null){
           writer.write( "欢迎首次登录");
        }else {
            String value = cookie.getValue();
            writer.write("哎呀深海哥宁来了,宁上次来的时候是"+value);
        }
        Cookie c = new Cookie("LastTime",new Date().toLocaleString().replaceAll(" ",""));
        c.setMaxAge(3*60*60*24);
        response.addCookie(c);
    }
}
